<?php

class Db_Tables2_TArea
{
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function GetProvinces($cityid_arr = NULL)
	{
		$result = array();
		
		$select = $this->dealers_db->select();
		$select->from("t_area", array("provinceId", "provinceName"));
		
		if($cityid_arr)
		{
			$select->where("cityId IN (?)", $cityid_arr);
		}
		
		$select->group("provinceId");
		$select->order("id ASC");
		
		$data = $this->dealers_db->fetchAll($select);
		
		if(!empty($data))
		{
			foreach($data as $d)
			{
				$result[$d['provinceId']] = $d['provinceName'];
			}
		}
		
		return $result;
	}
	
	function GetCities($provinceId, $type=1, $cityid_arr = NULL) //type=1 no limit type=2 limit in cityid_arr
	{
		$result = array();
		
		$select = $this->dealers_db->select();
		$select->from("t_area", array("cityId", "cityName"));
		
		if($provinceId)
		{
			$select->where("provinceId = ?", $provinceId);
		}
		
		if(2 == $type && $cityid_arr)
		{
			$select->where("cityId IN (?)", $cityid_arr);
		}
		
		$select->order("id ASC");
		
		$data = $this->dealers_db->fetchAll($select);
		
		if(!empty($data))
		{
			foreach($data as $d)
			{
				$result[] = array(
					'cityId' => $d['cityId'],
					'cityName' => $d['cityName']
					);
			}
		}
		
		return $result;
	}
	
	function GetNames($provinceId, $cityId, $countyId=null)
	{
		$result = array(
			"result" => false,
			"provinceId" => $provinceId,
			"provinceName" => "",
			"cityId" => $cityId,
			"cityName" => "",
			"countyId" => $countyId,
			"countyName" => ""
		);
		
		if($provinceId && $cityId)
		{
			$select = $this->dealers_db->select();
			$select->from("t_area", array("provinceName", "cityName"));
			$select->where("provinceId = ?", $provinceId);
			$select->where("cityId = ?", $cityId);
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				$result['provinceName'] = $row['provinceName'];
				$result['cityName'] = $row['cityName'];
				$result['result'] = true;
			}
			
			if($countyId)
			{
				$select = $this->dealers_db->select();
				$select->from("t_region", array("countyName"));
				$select->where("countyId = ?", $countyId);
				$row = $this->dealers_db->fetchRow($select);

				if($row)
				{
					$result['countyName'] = $row['countyName'];
				}
			}
		}
		
		return $result;
	}
	
	function GetAllCities($type=1)
	{
		$result = array();
		
		$select = $this->dealers_db->select();
		$select->from("t_area", array("cityId", "provinceName", "cityName"));
		$select->order(array("provinceLetter", "provinceName", "cityLetter"));
		$data = $this->dealers_db->fetchAll($select);
		
		if(!empty($data))
		{
			foreach($data as $d)
			{
				if(1 == $type)
				{
					$result[$d['cityId']] = $d['provinceName']."-".$d['cityName'];
				}elseif(2 == $type)
				{
					$result[$d['cityId']] = array("city_name" => $d['cityName']);
				}
			}
		}
		
		return $result;
	}
}